package com.wandian.audio.dialog;

import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.PopupWindow;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.mylib.libcore.utils.UIUtils;
import com.wandian.audio.R;
import com.wandian.audio.adapter.AudioPlayHistoryRecycleAdapter;
import com.wandian.audio.tools.MusicCollector;

/**
 * 音频小说目录弹框
 */
public class AudioHistoryListDialogWindow extends PopupWindow {

    private AudioPlayHistoryRecycleAdapter audioPlayHistoryRecycleAdapter;
    private View mMenuView;
    private RecyclerView recyclerView;
    public AudioDialogItemClink audioDialogItemClink;
    public interface  AudioDialogItemClink{
        void itemClink(int playIndex);
    }
    public AudioHistoryListDialogWindow(Context context, AudioDialogItemClink audioDialogItemClink) {
        super(context);
        this.audioDialogItemClink =audioDialogItemClink;
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mMenuView = inflater.inflate(R.layout.audio_dialog_catalog_view, null);
        recyclerView = (RecyclerView) mMenuView.findViewById(R.id.recycler);
        initRecycle(context);

     //   设置按钮监听
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            this.setClippingEnabled(false);
        }

        //设置SelectPicPopupWindow的View
        this.setContentView(mMenuView);
        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(LayoutParams.FILL_PARENT);
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        //设置SelectPicPopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.popwin_anim_style);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0xb0000000);
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        //mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
        if(!UIUtils.isEmpty(mMenuView)){
            mMenuView.setOnTouchListener(new OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    try {
                        int height = mMenuView.findViewById(R.id.pop_layout).getTop();
                        int y=(int) event.getY();
                        if(event.getAction()==MotionEvent.ACTION_UP){
                            if(y<height){
                                dismiss();
                            }
                        }
                        return true;
                    } catch (Exception e) {
                        dismiss();
                        e.printStackTrace();
                    }
                    return true;
                }
            });
        }

    }

    @Override
    public void showAsDropDown(View anchor) {
        if(Build.VERSION.SDK_INT >= 24) {
            Rect rect = new Rect();
            anchor.getGlobalVisibleRect(rect);
            int h = anchor.getResources().getDisplayMetrics().heightPixels - rect.bottom;
            setHeight(h);
        }
        super.showAsDropDown(anchor);
    }

    @Override
    public void showAsDropDown(View anchor, int xoff, int yoff) {
        if(Build.VERSION.SDK_INT >= 24) {
            Rect rect = new Rect();
            anchor.getGlobalVisibleRect(rect);
            int h = anchor.getResources().getDisplayMetrics().heightPixels - rect.bottom;
            setHeight(h);
        }
        super.showAsDropDown(anchor, xoff, yoff);
    }
    private void initRecycle(Context context){
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(context);
        recyclerView.setLayoutManager(layoutManager);

        audioPlayHistoryRecycleAdapter = new AudioPlayHistoryRecycleAdapter(R.layout.audio_item_history_view);
        recyclerView.setAdapter(audioPlayHistoryRecycleAdapter);
        audioPlayHistoryRecycleAdapter.setNewData(MusicCollector.getSongsList());
        audioPlayHistoryRecycleAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                audioDialogItemClink.itemClink(position);
                dismiss();
            }
        });
    }

}